<h1 class="right">إدارة الوحدات والتسكين</h1><!--<div class="h1-dot"></div>--><a href="#add_client" class="add_client_pop">
<img src="images/add-icon.png" align="left" class="left" alt=""/>
</a>

<div id="add_client" class="zoom-anim-dialog mfp-hide max-width800">
  <?php require_once("Apartments/Create.php"); ?>
</div>

<span class="left grey medium margin-left11 margin-top5"><?php echo date('g:ia d/m/Y')?></span>
<div class="clear"></div>
<div id="dashboard-container">
	<div class="apartment-filter">
    	<table class="bookings-filter">
        <tr>
          <form name="apartment_search" action="" method="GET" id="searchClient">

        	<td >الوحده &nbsp;:</td>
            <td style="width:84px;">&nbsp;
          <select name="apart_id" class="">
            
            <?php 
            if(isset($_SESSION['user_id']))
            {
            
              if(isset($binds['id']) && $binds['id'] == -1)
                echo "<option selected = 'selected' value='-1'>الجميع</option>";
              else
                echo "<option value='-1'>الجميع</option>";
                
              $aprtes = query("SELECT apart_name,id FROM apartments WHERE owner_id=?",$_SESSION['user_id']);
              foreach($aprtes as $aprt)
              {
                
                if(isset($binds['id']) && $binds['id'] == $aprt['id'] )
                  echo "<option selected='selected' value='{$aprt['id']}'>{$aprt['apart_name']}</option>";
                else
                  echo "<option  value='{$aprt['id']}'>{$aprt['apart_name']}</option>";
                
              }
            }
            ?>
          </select>
          </td><td style="width:10px;"></td>
          <td>الحالة&nbsp;:</td>
          <td style="width:84px;">&nbsp;
          <select name="apart_status">
            <?php 
              if(isset($binds['status']) && $binds['status'] == -1)
                echo "<option selected = 'selected' value='-1'>الجميع</option>";
              else
                echo "<option value='-1'>الجميع</option>";
                
              $status=query("SELECT * FROM apart_status");
			  
              foreach($status as $stat)
              {
                
                if(isset($binds['status']) && $binds['status'] == $stat['status_id'] )
                  echo "<option selected='selected' value='{$stat['status_id']}'>{$stat['status_txt']}</option>";
                else
                  echo "<option value='{$stat['status_id']}'>{$stat['status_txt']}</option>";
               
               
              }
            
            ?>
          </select>
          </td><td style="width:10px;"></td>
          <!--<td>نوع الايجار&nbsp;:</td>
          <td style="width:84px;">&nbsp;
            <select name="rent_type">
              <?php 
              /*if(isset($binds['status']) && $binds['status'] == -1)
                echo "<option selected = 'selected' value='-1'>الجميع</option>";
              else
                echo "<option value='-1'>الجميع</option>";*/
              ?>
              
              <option <?php //if(isset($binds['rent_type']) && $binds['rent_type'] == 0 ) {echo "selected='selected'";} ?> value="0">يومى</option>
              <option <?php //if(isset($binds['rent_type']) && $binds['rent_type'] == 1 ) {echo "selected='selected'";} ?> value="1">شهرى</option>
            </select>
         </td><td style="width:10px;"></td> -->
         <td>الغرف&nbsp;:</td>
         <td style="width:50px;">&nbsp;<input name="rooms#" type="text" class="auto-input" value="<?php if(isset($binds['rooms#']) ) {echo $binds['rooms#'];} ?>"/></td>
         <td style="width:10px;"></td>
         <td>الطابق&nbsp;:</td>
         <td style="width:50px;">&nbsp;<input name="floor#" type="text" class="auto-input" value="<?php if(isset($binds['floor#']) ) {echo $binds['floor#'];} ?>"/></td>
         <td style="width:10px;"></td>
         <td>النوع&nbsp;:</td>
         <td style="width:84px;">&nbsp;
          <select name="apart_type">
            <?php 
              if(isset($binds['apart_type']) && $binds['apart_type'] == -1)
                echo "<option selected = 'selected' value='-1'>الجميع</option>";
              else
                echo "<option value='-1'>الجميع</option>";
            
              
               $types = query("SELECT * FROM unit_type");
                   foreach($types as $type)
              {
                if(isset($binds['apart_type']) && $binds['apart_type'] == $type['type_id'] )
                  echo "<option selected = 'selected' value='{$type['type_id']}'>{$type['type_name']}</option>";
                else
                  echo "<option value='{$type['type_id']}'>{$type['type_name']}</option>";
               
               
              }
            
            ?>
          </select>
         </td ><td style="width:10px;"></td>
            <td style="width:73px;"><a href="#"><div class="search-botton">بحث</div></a></td>
        </form>
       
       </tr>
        </table>
    </div>
    
    
      <div class="dashboard-row">
    	<?php 
      if(!empty($aprts))
      {
        $j = 0;
        
        foreach($aprts as $aprt)
        {
          
      ?>
      <div class="apartments-cell relative cell-parent"><img src="images/home-<?php echo $aprt['status']; ?>.png" class="" align="middle" alt=""/>
            <p class="apartment-oName"><?php echo $aprt['apart_name']; ?></p>
            <div class="absolute apartment_details">
              
              <?php
                if($aprt['paid'] - $aprt['paid_back'] - $aprt['total'] - $aprt['services_revenue'] > 0 )
                {
                  echo "<p class='apartment-credit green'>";
                  echo "الرصيد: ".($aprt['paid'] - $aprt['paid_back'] - $aprt['total'] - $aprt['services_revenue']);
                }
                else if($aprt['paid'] - $aprt['paid_back'] - $aprt['total'] - $aprt['services_revenue'] < 0) 
                {
                  echo "<p class='apartment-credit red'>";
                  echo "الرصيد: ".($aprt['paid'] - $aprt['paid_back'] - $aprt['total'] - $aprt['services_revenue']);
                  
                }
                else
                {
                  echo "<p class='apartment-credit'>";
                }
              ?>
              </p>
              <div class="apartment-menu">
                <a href="#view_client<?php echo $j; ?>" class="view_client_pop">
                  <img src="images/view.png" class="right" align="" alt="View"/>
                </a>
                
                <div id="view_client<?php echo $j; ?>" class="zoom-anim-dialog mfp-hide view_client max-width800 ">
                  <?php require("Apartments/view.php"); ?>
                </div>
                      
                  <a href="#edit_client<?php echo $j; ?>" class="edit_client_pop">
                    <img src="images/edit.png" class="right margin-right5" align="" alt="Edit"/>
                  </a>
                  
                  <div id="edit_client<?php echo $j; ?>" class="zoom-anim-dialog mfp-hide view_client max-width800">
                    <?php require("Apartments/edit.php"); ?>
                  </div>
                  
                  <a href="#" class="delete"><img src="images/x.png" class="right margin-right5" align="" alt="Delete"/></a>
                  <input class="delete-id" type="hidden" value="<?php echo $aprt['id'] ?>"/>
                  <input class="filter-id" type="hidden" value="1"/> <!-- 1 means delete apartments -->
              </div>
            </div>
        </div>
      <?php 
        $j++;
        }
      }
      ?>
        

    </div>
    
        <?php
          
          $start = $page * 20 -19;
          $end  = $start + 19;
          if($end > $totalPages)
            $end = $totalPages;
          
          if(!empty($aprts))
          {
        ?>
        <div class="clear"></div>
        <div class="relative">
        <div id="apartments-selection"></div>
        <div class="xsmall showing-results absolute">عرض النتائج <?php echo $start; ?> - <?php echo $end; ?> من إجمالى <?php echo $totalPages; ?></div>
        </div> 
        <?php } ?>
        <table class="apartments-status">
        	<tr>
			<?PHP  
			$N=query("SELECT COUNT(*) FROM apartments");
			$M=query("SELECT COUNT(*) FROM apartments WHERE status=?",1);
			$F=query("SELECT COUNT(*) FROM apartments WHERE  status=?",2);
			$T=query("SELECT COUNT(*) FROM apartments WHERE  status=?",3);
			$S=query("SELECT COUNT(*) FROM apartments WHERE  status=?",4);
			$A=query("SELECT COUNT(*) FROM apartments WHERE  status=?",5);
			$B=query("SELECT COUNT(*) FROM apartments WHERE  status=?",6);
			?>
            	<td><img src="images/home-1-s.png" class="margin-side-5" align="" alt=""/>مؤجر [<?php echo $M[0]['COUNT(*)'];  ?>]</td>
                <td><img src="images/home-2-s.png" class="margin-side-5" align="" alt=""/>فارغ [<?php echo $F[0]['COUNT(*)'];  ?>]</td>
                <td><img src="images/home-3-s.png" class="margin-side-5" align="" alt=""/>تنظيف [<?php echo $T[0]['COUNT(*)'];  ?>]</td>
                <td><img src="images/home-4-s.png" class="margin-side-5" align="" alt=""/>صيانة [<?php echo $S[0]['COUNT(*)'];  ?>]</td>
                <td><img src="images/home-5-s.png" class="margin-side-5" align="" alt=""/>العقود المنتهية اليوم [<?php echo $A[0]['COUNT(*)'];  ?>]</td>
                <td><img src="images/home-6-s.png" class="margin-side-5" align="" alt=""/>محجوز بدون دخول [<?php echo $B[0]['COUNT(*)'];  ?>]</td>
				<?php  if($N[0]['COUNT(*)'] != 0){
		              	$RATE=(( $M[0]['COUNT(*)'] + $B[0]['COUNT(*)'] )/$N[0]['COUNT(*)'])*100; 
			   ?>
                <td style=""> 	نسبة الإشغال [<?php echo round($RATE, 2);   ?>]%</td>
				<?php } ?>
            </tr>
        </table>
        <div class="hidden totalPages"><?php if(isset($totalPages)) {echo $totalPages;} ?></div>
        <div class="hidden currentPage"><?php if(isset($page)) {echo $page;} ?></div>


</div>